/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ucmconfchecker.UI;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;
import org.jfree.data.general.DatasetGroup;
import ucmconfchecker.conformance.Event;
import ucmconfchecker.conformance.EventTrace;

/**
 *
 * @author Administrador
 */
public class EventView<T> extends ChartPanel {

    /**
     * Creates new form EventView
     */
    private EventTrace<T> events;
    public EventView() {
        super(null);
        initComponents();
        
    }
    
    private void update(){
        TaskSeriesCollection dataset = new TaskSeriesCollection();

        if(getEvents()!=null){
            List<Event<T>> eventList = new ArrayList<Event<T>>();
            eventList.addAll(getEvents());
            Collections.sort(eventList);
            TaskSeries known = new TaskSeries("known activations");
            TaskSeries unknown = new TaskSeries("known activations");
            int counter = 0;
            Map<T,Task> tasks = new HashMap<T,Task>();
            
            for(Event<T> event: eventList){
               if(event.getEvent()!=null){ 
                    Task eventTask = new Task(""+event.getEvent(), 
                    event.getTimeStart(), 
                    event.getTimeFinish());
                    if(tasks.containsKey(event.getEvent())){
                        tasks.get(event.getEvent()).addSubtask(eventTask);
                    }else{
                        tasks.put(event.getEvent(), eventTask);
                        known.add(eventTask);
                        unknown.add(new Task(""+event.getEvent(), 
                                             event.getTimeStart(), 
                                             event.getTimeStart()));
                        
                    }
                    
               } else{
                   Task eventTask = new Task("unknown", 
                    event.getTimeStart(), 
                    event.getTimeFinish());
                    eventTask.setPercentComplete(0);
                    known.add(eventTask);
               }
                   
            }
            dataset.add(known);
            

            // title, domain axis, range axis, dataset, legend, tooltip, urls
            JFreeChart chart = ChartFactory.createGanttChart("", "Activation", "Time", dataset, true, true, false);
            
            this.setChart(chart);
        }
    }
    public static JDialog showDialog(JFrame owner,EventTrace events){
        JDialog dialog = new JDialog(owner,"Event timeline");	
        EventView eventView = new EventView();
        eventView.setEvents(events);
        dialog.getContentPane().add(eventView, BorderLayout.CENTER);
        dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        dialog.setBounds(50, 50, 800, 200);
        dialog.setVisible(true);
        return dialog;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    /**
     * @return the events
     */
    public EventTrace<T> getEvents() {
        return events;
    }

    /**
     * @param events the events to set
     */
    public void setEvents(EventTrace<T> events) {
        this.events = events;
        update();
    }
}
